package com.hongyue.Dey01;

import java.util.Objects;

public class ChiHuo extends Thread {
    private BaoZi baozi;

    @Override
    public void run() {
        while (true) {
            synchronized (baozi) {
                if (!BaoZi.getBaozi()) {
                    try {
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(getName()+"开始吃"+BaoZi.getPi()+BaoZi.getXian()+"包子");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName()+"感觉很好吃想再来一个");
                BaoZi.setBaozi(false);
                baozi.notifyAll();
            }
        }
    }

    public ChiHuo(String name, BaoZi baozi) {
        super(name);
        this.baozi = baozi;
    }

    public ChiHuo() {
    }

    public BaoZi getBaozi() {
        return baozi;
    }

    public void setBaozi(BaoZi baozi) {
        this.baozi = baozi;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ChiHuo chiHuo = (ChiHuo) o;
        return Objects.equals(baozi, chiHuo.baozi);
    }

    @Override
    public int hashCode() {
        return Objects.hash(baozi);
    }

    @Override
    public String toString() {
        return "ChiHuo{" +
                "baozi='" + baozi + '\'' +
                '}';
    }
}
